package photobox.utils

/**
 * Created by sxing on 14-2-24.
 */
class ResultUtil {
    static def pageResults(rs,totalRow, pageParam,  fields = [:]) {
        def dataList = []
        if (fields.size()) {
            rs.each { r ->
                def data = [:]
                fields.each { k, v ->
                    def value = r[k]
                    if (value) {
                        data[v] = value
                    }
                }
                dataList.add(data)
            }
        } else {
            dataList = rs
        }

        def curPage = pageParam.page
        def pageSize = pageParam.pageSize
        if (curPage <= 0) curPage = 1
        if (pageSize <= 0) pageSize = 5
        if (pageSize >=100) pageSize = 100
        if (totalRow == null) totalRow = 0
        int totalPage = (totalRow +  pageSize -1) / pageSize
        return  [datas: dataList, records: totalRow, page: curPage, total: totalPage]
    }
}
